package tank.server.game;

import com.google.gson.Gson;
import tank.server.GameServerHandler;

import java.awt.event.KeyEvent;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class MyGame {

    public static final int PANEL_WIDTH = 1000;
    public static final int PANEL_HEIGHT = 750;
    public static final int GAME_HEIGHT = 850;
    public static final int DIRECTION_UP = 0;
    public static final int DIRECTION_DOWN = 1;
    public static final int DIRECTION_LEFT = 2;
    public static final int DIRECTION_RIGHT = 3;
    public ScheduledExecutorService threadPool;
    public Stage currentStage;
    private boolean gameOver;
    private Gson gson = new Gson();

    private static MyGame instance = null;

    private MyGame(){
        start();
    }

    public static MyGame getInstance(){
        if(instance == null){
            instance = new MyGame();
        }
        return instance;
    }

    public void handleClientCommand(String client,int cmd){
        System.out.println(client +"---" + cmd);
        Hero hero = currentStage.getHeros().get(client);

        switch(cmd){
            case KeyEvent.VK_UP:
                hero.setDirection(DIRECTION_UP);
                break;
            case KeyEvent.VK_DOWN:
                hero.setDirection(DIRECTION_DOWN);
                break;
            case KeyEvent.VK_LEFT:
                hero.setDirection(DIRECTION_LEFT);
                break;
            case KeyEvent.VK_RIGHT:
                hero.setDirection(DIRECTION_RIGHT);
                break;
            case KeyEvent.VK_SPACE:
                hero.fire();
                break;
        }

    }

    public void broadcast(){
        String s = gson.toJson(currentStage);
        GameServerHandler.channels.writeAndFlush(s);
    }

    private long lastTime = System.currentTimeMillis();

    public void start(){
        threadPool = Executors.newScheduledThreadPool(100);
        gameOver = false;
        currentStage = new Stage();
        //启动线程，定时广播游戏状态
        threadPool.execute(()->{
            while (!gameOver){
                for(String key : currentStage.getHeros().keySet()){
                    Hero hero = currentStage.getHeros().get(key);
                    hero.move();
                    for (String key2 : currentStage.getHeros().keySet()) {
                        Hero hero2 = currentStage.getHeros().get(key2);
                        if (!key.equals(key2)) {
                            tankHit(hero, hero2);
                        }
                    }
                }
                long now = System.currentTimeMillis();
                if(now - lastTime > 1000){
                    for(String key : currentStage.getHeros().keySet()){
                        Hero hero = currentStage.getHeros().get(key);
                        if(hero.getLive() <= 0){
                            currentStage.getHeros().remove(key);
                            currentStage.createHero(key);
                        }
                    }
                    lastTime = now;
                }
                broadcast();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //判断tank1是否击中tank2
    public void tankHit(Tank tank1,Tank tank2){
        Vector<Bullet> bullets = tank1.getBullets();
        for(Bullet bullet : bullets){
            if(!bullet.isRemoved() && bullet.hitTank(tank2)){
                tank2.setLive(tank2.getLive() - 1);
                bullet.setRemoved(true);
            }
        }
    }
}
